package com.woniuxy.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.crm.entity.Contact;
import com.woniuxy.crm.result.Result;
import com.woniuxy.crm.service.IContactService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户维护表 前端控制器
 *
 * @author 郑鹏
 * @since 2024-08-12
 */
@RestController
@RequestMapping("/admin/contact")
@Tag(name = "客户维护表接口")
public class ContactController {

    @Autowired
    private IContactService contactService;

    // 新增或者更新
    @PostMapping("saveOrUpdate")
    @Operation(summary = "新增或者修改")
    public Result save(@RequestBody Contact contact) {
        contactService.saveOrUpdate(contact);
        return Result.ok();
    }

    @DeleteMapping("DeleteById")
    @Operation(summary = "根据id删除")
    public Result delete(@RequestParam Long id) {
        contactService.removeById(id);
        return Result.ok();
    }

//    @PostMapping("/del/batch")
//    @Operation(summary = "批量删除")
//    public boolean deleteBatch(@RequestBody List<Integer> ids) {
//        return contactService.removeByIds(ids);
//    }

    @GetMapping("list")
    @Operation(summary = "查询全部")
    public Result<List<Contact>> findAll() {
        List<Contact> contacts = contactService.list();
        return Result.ok(contacts);
    }

    @GetMapping("getById")
    @Operation(summary = "根据id查询")
    public Result<Contact> findOne(@RequestParam Long id) {
        Contact contact = contactService.getById(id);
        return Result.ok(contact);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<Page<Contact>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<Contact> page = contactService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.ok(page);
    }

}

